import requests
import os,time
from bs4 import BeautifulSoup
url=[]
#批量生成学校链接
for i in range(1,108):
    link='http://school.zhongkao.com/province/1160/p%d' % (i)
    url.append(link)
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36','X-Requested-With':'XMLHttpRequest'}
for j in url:
    r=requests.get(url=j,headers=header)
    r.encoding='utf-8'
    soup=BeautifulSoup(r.text,'lxml')
    h3=soup.find_all('h3',limit=10)
    for k in h3:
    #将学校名字保存到本地文件夹中
        with open('上海高中.txt','a',encoding='utf-8') as f:
            f.write(k.text + '\n')
        print("成功保存:",k.text)
    time.sleep(15)
